package com.usian.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.admin.mapper.AdSensitiveMapper;
import com.usian.admin.service.AdSensitiveService;
import com.usian.utils.common.exception.CatchCustomException;
import com.usian.model.admin.dtos.SensitiveDto;
import com.usian.model.admin.pojos.AdSensitive;
import com.usian.model.common.dtos.PageResponseResult;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class AdSensitiveServiceImpl extends ServiceImpl<AdSensitiveMapper, AdSensitive> implements AdSensitiveService {

    @Override
    public ResponseResult list(SensitiveDto dto) {
        //1.检查参数
        if(dto==null){
            CatchCustomException.catchs(20002,"参数无效");
        }
        // 初始化分页
        dto.checkParam();
        Page<AdSensitive> page = new Page<>(dto.getPage(),dto.getSize());
        LambdaQueryWrapper<AdSensitive> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        // null ""
        lambdaQueryWrapper.like(StringUtils.isNotBlank(dto.getName()),AdSensitive::getSensitives,dto.getName());
        IPage<AdSensitive> ipage = this.page(page,lambdaQueryWrapper);
        ResponseResult responseResult = new PageResponseResult(dto.getPage(),dto.getSize(),(int)ipage.getTotal());
        responseResult.setData(ipage.getRecords());
        return responseResult;
    }

    @Override
    public ResponseResult insert(AdSensitive adSensitive) {
        //1.参数检查
        if(adSensitive == null){
            CatchCustomException.catchs(20002,"参数无效");
        }
        //维护创建时间
        adSensitive.setCreatedTime(new Date());
        this.save(adSensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


    @Override
    /**
     * 修改
     * @param adSensitive
     * @return
     */
    public ResponseResult update(AdSensitive adSensitive) {
        //1.参数检查
        if(adSensitive == null){
            CatchCustomException.catchs(20002,"参数无效");
        }
        if(adSensitive.getId() == null){
            CatchCustomException.catchs(20003,"Id不能为空");
        }
        if(this.getById(adSensitive.getId())==null){
            CatchCustomException.catchs(20004,"对象为空");
        }
        this.update(adSensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult deleteById(Integer id) {
        //1.参数检查
        if(id == null){
            CatchCustomException.catchs(20003,"Id不能为空");
        }
        if(this.getById(id)==null){
            CatchCustomException.catchs(20004,"对象为空");
        }
        this.removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
